package com.ai5suoai.ams.web.util;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * ResponseEntity的工具类
 *
 * @author chenzhenjia
 * @since 16/4/1
 */
@Component
public class ResponseEntityUtil {
    private static final String KEY_MSG = "msg";
    private static final String KEY_STATUS = "status";

    private Map<String, Object> generatorSuccess(String msg) {
        return new HashMap<String, Object>(2) {{
            put(KEY_STATUS, true);
            put(KEY_MSG, msg);
        }};
    }

    private Map<String, Object> generatorFail(String msg) {
        return new HashMap<String, Object>(2) {{
            put(KEY_STATUS, false);
            put(KEY_MSG, msg);
        }};
    }


    /**
     * 返回 BAD_REQUEST 的ResponseEntity的方法
     *
     * @param msg 消息
     */
    public ResponseEntity badRequest(String msg) {
        return fail(HttpStatus.BAD_REQUEST, msg);
    }

    public ResponseEntity success(HttpStatus status, String msg) {
        return ResponseEntity.status(status).body(generatorSuccess(msg));
    }

    /**
     * 服务器内部错误
     */
    public ResponseEntity serverError() {
        return fail(HttpStatus.INTERNAL_SERVER_ERROR, "未知错误");
    }

    public ResponseEntity fail(HttpStatus status, String msg) {
        return ResponseEntity.status(status).body(generatorFail(msg));
    }


}
